import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';
import '../common/utils.dart';
import '../controllers/topic_detail_list_view_controller.dart';
import '../model/community_video_model.dart';
import '../widgets/tag_list_view.dart';

class TopicDetailListView extends StatelessWidget {
  final IssueList item;

  const TopicDetailListView({super.key, required this.item});

  _buildBottomItem() {
    return Column(
      children: [
        SizedBox(
          height: 15,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Expanded(
              child: Row(
                children: [
                  Icon(
                    size: 16,
                    Icons.favorite_border,
                    color: Colors.grey[400],
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      "111",
                      style: TextStyle(fontSize: 12, color: Colors.grey[400]),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Row(
                children: [
                  Icon(
                    size: 16,
                    Icons.textsms_outlined,
                    color: Colors.grey[400],
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      "56",
                      style: TextStyle(fontSize: 12, color: Colors.grey[400]),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Row(
                children: [
                  Icon(
                    size: 16,
                    Icons.star_border,
                    color: Colors.grey[400],
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      "收藏",
                      style: TextStyle(fontSize: 12, color: Colors.grey[400]),
                    ),
                  ),
                ],
              ),
            ),
            Row(
              children: [
                Icon(
                  size: 16,
                  Icons.share,
                  color: Colors.grey[400],
                ),
                Padding(
                  padding: EdgeInsets.only(left: 4),
                  child: Text(
                    "分享",
                    style: TextStyle(fontSize: 12, color: Colors.grey[400]),
                  ),
                ),
              ],
            )
          ],
        ),
        SizedBox(
          height: 20,
        ),
        Divider(
          height: 1,
          color: Colors.grey[100],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(TopicDetailListViewController(item), tag: item.id.toString());

    final time = formatDate(item.data?.header?.time ?? 0);
    final description = item.data?.content?.data.description ?? '';
    final maxLines = 2;
    final maxWidth = Get.width - 30;

    final span = TextSpan(
      text: description,
      style: TextStyle(
        fontSize: 14,
      ),
    );

    final tp = TextPainter(
      text: span,
      maxLines: maxLines,
      textDirection: TextDirection.ltr,
    )..layout(maxWidth: maxWidth);

    // 判断文字是否超出2行
    final isOverflowing = tp.didExceedMaxLines;

    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              SizedBox(
                width: 45,
                height: 45,
                child: Stack(
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.circular(20),
                      child: CachedNetworkImage(
                        fit: BoxFit.cover,
                        fadeInDuration: const Duration(milliseconds: 100),
                        fadeOutDuration: const Duration(seconds: 0),
                        imageUrl: item.data?.header?.icon ?? '',
                        width: 40,
                        height: 40,
                      ),
                    ),
                    Positioned(
                      right: 4,
                      bottom: 6,
                      width: 10,
                      height: 10,
                      child: Image.asset(
                        'assets/images/icon_author.png',
                        fit: BoxFit.cover,
                      ),
                    )
                  ],
                ),
              ),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: 5, vertical: 10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item.data?.header?.issuerName ?? '',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(color: Colors.black.withOpacity(.72), fontSize: 14, fontWeight: FontWeight.bold),
                      ),
                      Row(
                        children: [
                          Text(
                            '$time 发布:',
                            style: TextStyle(
                              fontSize: 12,
                              color: Colors.black.withOpacity(.3),
                            ),
                          ),
                          SizedBox(
                            width: 5,
                          ),
                          Expanded(
                            child: Text(
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              item.data?.content?.data.title ?? '',
                              style: TextStyle(
                                fontSize: 13,
                                color: Colors.black,
                              ),
                            ),
                          )
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
          Obx(() => RichText(
                text: controller.isExpanded.value
                    ? TextSpan(
                        text: description,
                        style: TextStyle(color: Colors.black.withOpacity(.6)),
                      )
                    : TextSpan(
                        children: [
                          TextSpan(
                            text: isOverflowing ? description.substring(0, tp.getPositionForOffset(Offset(tp.width - 30, tp.height)).offset) : description,
                          ),
                          if (isOverflowing)
                            TextSpan(
                              text: ' 更多',
                              style: TextStyle(color: Colors.blue),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  controller.expandText();
                                },
                            ),
                        ],
                        style: TextStyle(color: Colors.black.withOpacity(.6)),
                      ),
              )),
          TagListView(tags: item.data?.content?.data.tags ?? []),
          GestureDetector(
            child: AspectRatio(
              aspectRatio: (Get.width - 20) / 200,
              child: Obx(
                () => controller.isPlaying.value
                    ? VideoPlayer(controller.videoController)
                    : Stack(
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(8),
                            child: CachedNetworkImage(
                              fit: BoxFit.cover,
                              fadeInDuration: const Duration(milliseconds: 100),
                              fadeOutDuration: const Duration(seconds: 0),
                              imageUrl: item.data?.content?.data.cover?.detail ?? '',
                              width: double.infinity,
                              height: 200,
                            ),
                          ),
                          SizedBox(
                            width: double.infinity,
                            height: 200,
                            child: Center(
                              child: Icon(Icons.play_arrow_sharp, color: Colors.white, size: 50),
                            ),
                          )
                        ],
                      ),
              ),
            ),
            onTap: () {
              controller.onTogglePlayPauseVideo();
            },
          ),
          _buildBottomItem()
        ],
      ),
    );
  }
}
